home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / GPPCONIO.C < prev    next >
C/C++ Source or Header  |  1992-03-29  |  7KB  |  327 lines

  1. /**********************************************************************
  2.  *  
  3.  *  NAME:           gppconio.c
  4.  *  
  5.  *  DESCRIPTION:    simulate Borland text video funcs for GNU C++
  6.  *  
  7.  *  copyright (c) 1991 J. Alan Eldridge
  8.  * 
  9.  *  M O D I F I C A T I O N   H I S T O R Y
  10.  *
  11.  *  when        who                 what
  12.  *  -------------------------------------------------------------------
  13.  *  10/27/91    J. Alan Eldridge    created
  14.  *
  15.  *********************************************************************/
  16.  
  17. #include    <stdlib.h>
  18. #include    <stdarg.h>
  19.  
  20. #include    <pc.h>
  21. #include    "gppconio.h"
  22.  
  23. #define DBGGTINFO   0
  24.  
  25. static struct text_info txinfo;
  26.  
  27. #define VIDADDR(r,c) (ScreenPrimary + r * txinfo.screenwidth + c)
  28.  
  29. int puttext(int c, int r, int c2, int r2, void *buf)
  30. {
  31.     int     c0;
  32.     short   *pvch = (short *)buf;
  33.     
  34.     r--, r2--, c--, c2--;
  35.     for (c0 = c; r <= r2; r++) {
  36.         for (c = c0; c <= c2; c++)
  37.             *VIDADDR(r,c) = *pvch++;
  38.     }
  39.     return 1;
  40. }
  41.  
  42. int gettext(int c, int r, int c2, int r2, void *buf)
  43. {
  44.     int     c0;
  45.     short   *pvch = (short *)buf;
  46.     
  47.     r--, r2--, c--, c2--;
  48.     for (c0 = c; r <= r2; r++) {
  49.         for (c = c0; c <= c2; c++)
  50.             *pvch++ = *VIDADDR(r,c);
  51.     }
  52.     return 1;
  53. }
  54.         
  55. void gotoxy(int col, int row)
  56. {
  57.     ScreenSetCursor(row + txinfo.wintop - 2, col + txinfo.winleft - 2);
  58. }
  59.  
  60. int wherex(void)
  61. {
  62.     int row, col;
  63.     
  64.     ScreenGetCursor(&row, &col);
  65.  
  66.     return col - txinfo.winleft + 2;
  67. }
  68.     
  69. int wherey(void)
  70. {
  71.     int row, col;
  72.     
  73.     ScreenGetCursor(&row, &col);
  74.  
  75.     return row - txinfo.wintop + 2;
  76. }
  77.     
  78. void textmode(int unused_mode)
  79. {
  80. }
  81.  
  82. void textattr(int attr)
  83. {
  84.     ScreenAttrib = (unsigned char)attr;
  85. }
  86.  
  87. void textcolor(int color)
  88. {
  89.     ScreenAttrib &= 0xf0;
  90.     ScreenAttrib |= (color & 0x0f);
  91. }
  92.  
  93. void textbackground(int color)
  94. {
  95.     ScreenAttrib &= 0x0f;
  96.     ScreenAttrib |= ((color & 0x0f) << 4);
  97. }
  98.  
  99. void highvideo(void)
  100. {
  101.     ScreenAttrib |= 0x08;
  102. }
  103.  
  104. void lowvideo(void)
  105. {
  106.     ScreenAttrib &= 0x07;
  107. }
  108.  
  109. void normvideo(void)
  110. {
  111.     ScreenAttrib = txinfo.normattr;
  112. }
  113.  
  114. void _setcursortype(int unused_type)
  115. {
  116. }
  117.  
  118. static void getwincursor(int *row, int *col)
  119. {
  120.     ScreenGetCursor(row, col);
  121.  
  122.     *col += txinfo.winleft - 1;
  123.     *row += txinfo.wintop - 1;
  124. }
  125.  
  126. void clreol(void)
  127. {
  128.     short   image[ 256 ];
  129.     short   val = ' ' | (ScreenAttrib << 8);
  130.     int     c, row, col, ncols;
  131.     
  132.     getwincursor(&row, &col);
  133.     ncols = txinfo.winright - col;
  134.  
  135.     for (c = 0; c < ncols; c++)
  136.         image[ c ] = val;
  137.     
  138.     puttext(col + 1, row + 1, txinfo.winright, row + 1, image);
  139. }
  140.  
  141. static void fillrow(int row, int left, int right, int fill)
  142. {
  143.     int col;
  144.     
  145.     for (col = left; col <= right; col++)
  146.         *VIDADDR(row, col) = fill;
  147. }
  148.  
  149. static void scrollwin(int t, int b, int l)
  150. {
  151.     int top = txinfo.wintop + t - 1, left = txinfo.winleft - 1,
  152.         bot = txinfo.wintop + b - 1, right = txinfo.winright - 1;
  153.     
  154.     int row, col, nbytes, fill, nlines;
  155.     
  156.     if (!l) 
  157.         return;
  158.     
  159.     nbytes = (right - left + 1) * 2;
  160.     nlines = (bot - top + 1) - l;
  161.  
  162.     fill = ' ' | (ScreenAttrib << 8);
  163.  
  164.     if (l > 0) {
  165.         for (row = top; nlines > 0; row++, nlines--)
  166.             memcpy(VIDADDR(row, left), VIDADDR(row + l, left), nbytes);
  167.         for (; row <= bot; row++)
  168.             fillrow(row, left, right, fill);
  169.     } else {
  170.         for (row = bot; nlines > 0; row--, nlines--)
  171.             memcpy(VIDADDR(row, left), VIDADDR(row + l, left), nbytes);
  172.         for (; row >= top; row--)
  173.             fillrow(row, left, right, fill);
  174.     }
  175. }
  176.  
  177. void clrscr(void)
  178. {
  179.     scrollwin(0, txinfo.winbottom - txinfo.wintop, 32000);
  180. }
  181.  
  182. int putch(int c)
  183. {
  184.     int     row, col;
  185.     
  186.     getwincursor(&row, &col);
  187.  
  188.     /*  first, handle the character */
  189.  
  190.     if (c == '\n')
  191.         row++;
  192.     else if (c == '\r')
  193.         col = txinfo.winleft - 1;
  194.     else {
  195.         short   val = c | (ScreenAttrib << 8);
  196.  
  197.         puttext(col + 1, row + 1, col + 1, row + 1, &val);
  198.         col++;
  199.     }
  200.     
  201.     /* now, readjust the window     */
  202.     
  203.     if (col >= txinfo.winright) {
  204.         col = txinfo.winleft - 1;
  205.         row++;
  206.     }
  207.     
  208.     if (row >= txinfo.winbottom) {
  209.         scrollwin(0, txinfo.winbottom - txinfo.wintop, 1);
  210.         row--;
  211.     }
  212.     
  213.     ScreenSetCursor(row, col);
  214.     return c;
  215. }
  216.  
  217. int getche(void)
  218. {
  219.     int c = getch();
  220.     
  221.     if (!c || c == 0xE0)
  222.         c = getch();
  223.     
  224.     return putch(c);
  225. }
  226.  
  227. void delline(void)
  228. {
  229.     scrollwin(wherey() - 1, txinfo.winbottom - txinfo.wintop, 1);
  230. }
  231.  
  232. void insline(void)
  233. {
  234.     scrollwin(wherey() - 1, txinfo.winbottom - txinfo.wintop, -1);
  235. }
  236.  
  237. void window(int left, int top, int right, int bottom)
  238. {
  239.     if (top < 1 || left < 1 || right > txinfo.screenwidth ||
  240.             bottom > txinfo.screenheight)
  241.         return;
  242.     
  243.     txinfo.wintop = top;
  244.     txinfo.winleft = left;
  245.     txinfo.winright = right;
  246.     txinfo.winbottom = bottom;
  247. }
  248.  
  249. int cputs(const char *s)
  250. {
  251.     int c = 0;
  252.     
  253.     while (*s)
  254.         putch(c = *s++);
  255.     
  256.     return c;
  257. }
  258.  
  259. int cprintf(const char *fmt, ...)
  260. {
  261.     int     cnt;
  262.     char    buf[ 2048 ];
  263.     va_list ap;
  264.     
  265.     va_start(ap, fmt);
  266.     cnt = vsprintf(buf, fmt, ap);
  267.     va_end(ap);
  268.     
  269.     cputs(buf);
  270.     return cnt;
  271. }
  272.     
  273. int movetext(int left, int top, int right, int bottom, int dleft, int dtop)
  274. {
  275.     char    *buf = malloc((right - left + 1) * (bottom - top + 1) * 2);
  276.     
  277.     if (!buf)
  278.         return 0;
  279.     
  280.     gettext(left, top, right, bottom, buf);
  281.     puttext(dleft, dtop, dleft + right - left, dtop + bottom - top, buf);
  282.     free(buf);
  283.     return 1;
  284. }
  285.  
  286. static void _gettextinfo(struct text_info *t)
  287. {
  288.     int row, col;
  289.     
  290.     t->winleft = t->wintop = 1;
  291.     t->winright = t->screenwidth = ScreenCols();
  292.     t->winbottom = t->screenheight = ScreenRows();
  293.     t->normattr = 0x07;
  294.     t->currmode = ScreenMode();
  295.     ScreenGetCursor(&row, &col);
  296.     t->curx = col;
  297.     t->cury = row;
  298.  
  299. #if DBGGTINFO
  300.     printf("left=%2d,right=%2d,top=%2d,bottom=%2d\n",t->winleft,
  301.         t->winright,t->wintop,t->winbottom);
  302.     printf("scrht=%2d,scrwid=%2d,norm=%2x,mode=%2d,x=%2d,y=%2d\n",
  303.         t->screenheight, t->screenwidth, t->normattr, t->currmode,
  304.         t->curx, t->cury);
  305. #endif
  306. }
  307.  
  308. void gettextinfo(struct text_info *t)
  309. {
  310.     *t = txinfo;
  311. #if DBGGTINFO
  312.     printf("left=%2d,right=%2d,top=%2d,bottom=%2d\n",t->winleft,
  313.         t->winright,t->wintop,t->winbottom);
  314.     printf("scrht=%2d,scrwid=%2d,norm=%2x,mode=%2d,x=%2d,y=%2d\n",
  315.         t->screenheight, t->screenwidth, t->normattr, t->currmode,
  316.         t->curx, t->cury);
  317. #endif
  318. }
  319.  
  320. extern int _gppconio_init;
  321.  
  322. void gppconio_init(void)
  323. {
  324.     _gettextinfo(&txinfo);
  325.     _gppconio_init = 1;
  326. }
  327.